package com.example.eduservice.controller;


//import com.example.eduservice.entity.EduTeacher;
//import com.example.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.R;
import com.example.eduservice.entity.EduTeacher;
import com.example.eduservice.entity.vo.TeacherQuery;
import com.example.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-10-09
 */
@Slf4j
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin //解决跨域问题
public class EduTeacherController {

    //将service进行注入
    @Autowired

    private EduTeacherService teacherService;

    /**
     * 查询所有的讲师信息
     * @return
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        //调用service的方法实现查询所有的功能
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 逻辑删除讲师
     * @return
     */
    @ApiOperation(value = "根据 ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 分页查询讲师的方法
     * @param current：当前页
     * @param limit：每页的记录数
     * @return
     */
    @ApiOperation(value = "分页查询讲师的方法")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        //调用方法实现分页
        teacherService.page(teacherPage,null);
        long total = teacherPage.getTotal(); //总记录数
        List<EduTeacher> records = teacherPage.getRecords(); //数据list的集合
        //第一种方式
//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        //链式结构，第二种方式
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 条件查询带分页的方法
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pageTeacherCondition/{current}/{limit}")  //这里是将TeacherQuery作为一个对象，把条件值封装到对象里面，把对象传递到接口里面
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @ApiParam(name = "teacherQuery",value = "前端传到后端封装的四个对象",required = true)
                                  @RequestBody(required = false) TeacherQuery teacherQuery){//TeacherQuery是前端传过来的数据所以不用写mapper，当成对象就行
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mabitis学过的 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //进行匹配和判断
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            //ge大于等于
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            //le小于等于
            wrapper.le("gmt_modified",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        teacherService.page(teacherPage,wrapper);
        long total = teacherPage.getTotal(); //总记录数
        List<EduTeacher> records = teacherPage.getRecords(); //数据list的集合
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 添加讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**根据讲师id进行查询
     * 进行一个回显的操作
     * @param id
     * @return
     */
    @ApiOperation(value = "根据讲师id进行查询（回显操作）")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    /**
     * 修改讲师
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "teacherQuery",value = "前端传到后端封装的要修改的内容",required = true)
            @RequestBody EduTeacher eduTeacher){
        //回显操作
        //前端不止要拿到一个id,还要拿到所有前端传过来要修改的数据
        //根据id进行修改(intro，career，level，avatar，sort)
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

